package com.ts.api.module.api.convert.platform.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.gitee.apanlh.util.base.BigDecimalUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.module.api.entity.ApiProtocol;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 *  默认平台撞库返回
 *
 *  @author Pan
 */
@Data
public class RespPlatformPhoneMd5DataExchange {

    /** 主体名称/公司名称 */
    private String productName;
    /** 投放名称-简称 */
    @JSONField(alternateNames = {"companyName"})
    private String displayName;
    /** 展示lOGO地址 */
    private String logoPath;
    /** 返回价格 */
    private BigDecimal price;
    /** 订单号 */
    private String applyId;
    /** 平台返回的回调页面 */
    @JSONField(alternateNames = {"url"})
    private String callUrl;
    /** 协议集合 */
    private List<ApiProtocol> protocolList;
    /** 是否业务判断成功 */
    @JSONField(serialize = false)
    private Boolean hasBizSuc = false;

    /**
     *  构建失败
     *
     *  @return RespPlatFormDataExchangePhoneMd5
     */
    public static RespPlatformPhoneMd5DataExchange ofFail() {
        RespPlatformPhoneMd5DataExchange respPlatformPhoneMd5DataExchange = new RespPlatformPhoneMd5DataExchange();
        respPlatformPhoneMd5DataExchange.setHasBizSuc(false);
        return respPlatformPhoneMd5DataExchange;
    }

    /**
     *  构建成功
     *
     *  @return RespPlatFormDataExchangePhoneMd5
     */
    public static RespPlatformPhoneMd5DataExchange ofSuc() {
        RespPlatformPhoneMd5DataExchange respPlatformPhoneMd5DataExchange = new RespPlatformPhoneMd5DataExchange();
        respPlatformPhoneMd5DataExchange.setHasBizSuc(true);
        return respPlatformPhoneMd5DataExchange;
    }


    /**
     *  计算分润价
     *
     *  @param  productPlanShare        平台计划分润比例
     *  @return String
     */
    public BigDecimal calculateSharePrice(BigDecimal productPlanShare) {
        BigDecimal percent = BigDecimalUtils.division(productPlanShare, 100);
        //  原价 * 渠道分成份额% = 分润后的单价
        BigDecimal sharePrice = BigDecimalUtils.multiply(this.getPrice(), percent).setScale(2, RoundingMode.DOWN);
        return sharePrice;
    }

    /**
     *  根据计划撞库成功响应体生成平台计划实体类
     *
     *  @param  respJson    响应实体
     *  @return ApiPlatFormDataExchange
     */
    public static RespPlatformPhoneMd5DataExchange parseByJson(String respJson) {
        if (ValidParam.isEmpty(respJson)) {
            return null;
        }
        return JsonUtils.toBean(respJson, RespPlatformPhoneMd5DataExchange.class);
    }
}
